package filecheck;

import java.io.*;

/**
 * Created by xu on 2015/8/12.
 */
public class SensorFileCheck {
    public static void main(String[] v) {
        BufferedReader fis;
        BufferedWriter bw;
        String lastTime = "";
        StringBuffer stringBuffer;
        try {
            fis = new BufferedReader(
                    new InputStreamReader(new FileInputStream(new File("D://sensorCollection.wifi"))));
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D://sensorData.sensor"))));

            stringBuffer = new StringBuffer();
            String line = fis.readLine();
            int i = 0;
            while (line != null) {
                if (line.contains("#")) {
                    if (!lastTime.equals(line.substring(line.lastIndexOf("#") + 1))) {
                        lastTime = line.substring(line.lastIndexOf("#") + 1);
                        stringBuffer.append("\n");
                        bw.write(stringBuffer.toString());
                        bw.flush();
                        line = line.substring(line.indexOf("#") + 1);
                        stringBuffer.append(lastTime).append("    ");
                        stringBuffer.append(line.substring(0, line.indexOf("#"))).append("   ");
                        line = line.substring(line.indexOf("#") + 1);
//                        System.out.println(line);
                        stringBuffer.append(line.substring(0, line.indexOf("#")));
                        stringBuffer.append("\n");
                    } else {
                        line = line.substring(line.indexOf("#") + 1);
                        stringBuffer.append(line.substring(0, line.indexOf("#"))).append("   ");
                    }
                }
                i++;
                System.out.println(i);
                line = fis.readLine();
            }
            System.out.println("It`s over");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
